#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/3/21 18:50
# @Author  : huidong.bai
# @File    : Run_Mongo.py
# @Software: PyCharm
# @Mail    : MasterBai2018@outlook.com
import json
import os
import argparse
import signal
import sys
import psutil
from psutil import NoSuchProcess
from src.userInterface.run_test import run_all_test
from src.core.Common.Bean.Conveyor import Conveyor
import faulthandler
# 在import之后直接添加以下启用代码即可
faulthandler.enable()
cmder = Conveyor()


def signal_handler(signal, frame):
    try:
        p = psutil.Process()
        p_list = p.children(recursive=True)
        for i in p_list:
            i.kill()
    except NoSuchProcess as e:
        pass
    sys.exit(0)


root_dir = os.path.abspath(os.path.dirname(__file__))


def init_input():
    tester_parser = argparse.ArgumentParser(
        description='Mongo automatic testing.')
    tester_parser.add_argument(
        '-c', '--mongo_solution', type=str, default='null', help='path to test solution file.')
    tester_parser.add_argument(
        '-t', '--mongo_tag', type=str, default=None, help="""--mongo_tag=[ONLINE][OFFLINE].
            Run the tests specified by the label, eg: [ONLINE][OFFLINE], means,
            It will execute as long as the suite contains one of the tags-[ONLINE][OFFLINE].
            and the tags that '+' at the beginning, means the suite must contain the tag to be executed.
            No tag means run the tests specified by --mongo_solution.""")
    tester_parser.add_argument(
        '-w', '--mongo_work_space', type=str, default="workspace", help="""--mongo_work_space=[WORK_PATH].
            Run the tests in the specified work space.""")
    tester_parser.add_argument(
        '-s', '--mongo_rescue', action="store_true", help="""--mongo_resuce
            The parameter does't need input, its value is True when the parameter response.
            To rescue the report when an error occurs.""")
    tester_parser.add_argument(
        '-l', '--mongo_list_suites', action="store_true", help="""--mongo_list_suites
            The parameter does't need input, its value is True when the parameter response.
            List the names of all testsuites instead of running them. """)
    tester_parser.add_argument(
        '-f', '--mongo_filter', type=str, default='null', help="""--mongo_filter=[POSTIVE_PATTERNS][-NEGATIVE_PATTERNS].
            Run only the tests whose name matches one of the positive patterns but
            none of the negative patterns. '?' matches any single character; '*'
            matches any substring; ':' separates two patterns.""")
    tester_parser.add_argument(
        '-C', '--mongo_filter_config', type=str, default=root_dir, help="""--mongo_filter=[CONF_PATH].
            The AIBS-SDK config. The parameter must been given when the arguement of --mongo_filter isn't null.""")
    tester_parser.add_argument(
        '-a', '--mongo_case', type=str, default=None, help="""--mongo_case=[case.list].
            This parameter is a supplementary term for per specified suite.
            it will replace the caselist specified in config when it's not none""")
    tester_parser.add_argument(
        '-p', '--mongo_project', type=str, default=None, help="""--mongo_project=[SCENARIO_CONF].
            This parameter is a supplementary term for per specified suite.""")
    tester_parser.add_argument(
        '-r', '--mongo_report', type=str, default='Mongo_Report', help="""--mongo_report=[NAME].
            Generate the report for this test task.""")
    tester_parser.add_argument(
        '-e', '--mongo_email', action="store_true", help="""--mongo_email
            The parameter does't need input, its value is True when the parameter response.
            To send email in necessity.""")
    tester_parser.add_argument(
        '-k', '--mongo_voice_seek', type=str, default=None, help="""--mongo_voice_seek=[voice's index seek].
            This parameter use to configure where to read the audio""")
    tester_parser.add_argument(
        '-F', '--mongo_filter_case_line', type=str, default=None, help="""--mongo_filter_case_line=[case's line in list].
            This parameter use to specify the number of lines in the caselist""")
    tester_parser.add_argument(
        '-G', '--mongo_generate_case', type=str, default=None, help="""--mongo_generate_case=[skill:xxx-intention:xxx].
            This parameter use to generate new caselist""")
    tester_parser.add_argument(
        '-E', '--mongo_enable', type=str, default=None, help="""--mongo_enable=[FOUNCTION].
            This parameter use to enable the 'DEFINE' in solution""")
    tester_parser.add_argument(
        '-S', '--mongo_dependent_resources', type=str, default="solution_filter", help="""--mongo_dependent_resources=[FOUNCTION].
            This parameter use to specify some dependent resources""")
    tester_parser.add_argument(
        '-u', '--mongo_update_common_resource', action="store_true", help="""--mongo_update_common_resource.
            The parameter does't need input, its value is True when the parameter response
            To update running suite's common resource""")
    tester_parser.add_argument(
        '-V', '--mongo_detail_version', action="store_true", help="""--mongo_detail_version.
            The parameter does't need input, its value is True when the parameter response
            To switch detailed information mode.""")
    tester_parser.add_argument(
        '-g', '--mongo_gdb', action="store_true", default=False, help="""--mongo_gdb 
            The parameter does't need input, its value is True when the parameter response.
            Gdb for this test task.""")
    tester_parser.add_argument(
        '-D', '--mongo_ftp_downloader', type=int, default=None, help="""--mongo_ftp_downloader.
                This parameter use to download res option: 
                    0: Download audio and resources
                    1: Download audio only
                    2: Download resources only
                Download the ftp res when the parameter exist.""")
    tester_parser.add_argument(
        '-J', '--mongo_library_downloader', type=str, default=None, help="""--mongo_library_downloader.
                This parameter is used to automatically download the corresponding library, 
                value is construction number or new. If it is new, the default value is the latest successfully built library""")
    tester_parser.add_argument(
        '-M', '--mongo_message_wechat', action="store_true", help="""--mongo_message_wechat.
                This parameter is used to send enterprise WeChat notifications after testing is completed, 
                without the need to enter a value. When there is a parameter, notifications are sent, 
                and when there is no parameter, notifications are not sent.""")
    tester_parser.add_argument(
        '-L', '--mongo_log_save_option', type=str, default="yes", help="""--mongo_log_save_option.
                    This parameter use to set log save option:
                        yes: report save the sdk log.
                        no: report not save the sdk log.
                    the default option is yes""")
    tester_parser.add_argument(
        '-d', '--mongo_docker_option', type=str, default="no", help="""--mongo_docker_save_option.
                    This parameter use to set docker images""")
    tester_parser.add_argument(
        '-b', '--mongo_check_project', type=str, default="24MM", help="""--mongo_check_bev 
            The parameter is chose environment bev""")
    tester_args = tester_parser.parse_args()
    return tester_args


if __name__ == '__main__':
    signal.signal(signal.SIGINT, signal_handler)
    args = init_input()
    print("start to run all tests")
    run_all_test(args, root_dir)
    print("run all tests finish")
